/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chess;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Alejandro
 */
public class TokenController extends Thread implements ActionListener {
    int i;
    int j;
    int n;
    int[] select;
    JButton [][] board;
    Token [][] tokens;
    String [] letras = new String [26];

    public TokenController (int j, int i, JButton [][] tablero, Token[][] fichas, int[] select) {
        this.i=i;
        this.j=j;
        this.board=tablero;
        this.tokens = fichas;
        this.select = select;
        this.n = 4;
        letras[0]="a";letras[4]="e";letras[8]="i";letras[12]="m";letras[16]="q";letras[20]="u";letras[24]="y";
        letras[1]="b";letras[5]="f";letras[9]="j";letras[13]="n";letras[17]="r";letras[21]="v";letras[25]="z";
        letras[2]="c";letras[6]="g";letras[10]="k";letras[14]="o";letras[18]="s";letras[22]="w";
        letras[3]="d";letras[7]="h";letras[11]="l";letras[15]="p";letras[19]="t";letras[23]="x";
    }

    @Override
    public void actionPerformed( ActionEvent evento ){
        //movimiento de las fichas
        if (select[0] == 1 && (select[1] != j || select[2] != i)){
            if (tokens[select[1]][select[2]] != null){
                if (select[3] == tokens[select[1]][select[2]].getColor()){
                    Token[][] oldTokens = new Token[n][n];
                    for (int fila=0; fila<n; fila++){
                        for (int columna=0; columna<n; columna++){
                            if (tokens[fila][columna]!=null){
                                if (tokens[fila][columna].getType().equals("K")){
                                    oldTokens[fila][columna] = new King(fila,columna,tokens[fila][columna].getColor(),board,oldTokens);
                                }
                                else if (tokens[fila][columna].getType().equals("Q")){
                                    oldTokens[fila][columna] = new Queen(fila,columna,tokens[fila][columna].getColor(),board,oldTokens);
                                }
                                else if (tokens[fila][columna].getType().equals("N")){
                                    oldTokens[fila][columna] = new Knight(fila,columna,tokens[fila][columna].getColor(),board,oldTokens);
                                }
                                else if (tokens[fila][columna].getType().equals("R")){
                                    oldTokens[fila][columna] = new Rook(fila,columna,tokens[fila][columna].getColor(),board,oldTokens);
                                }
                                else if (tokens[fila][columna].getType().equals("B")){
                                    oldTokens[fila][columna] = new Bishop(fila,columna,tokens[fila][columna].getColor(),board,oldTokens);
                                }
                                else{
                                    oldTokens[fila][columna] = new Pawn(fila,columna,tokens[fila][columna].getColor(),board,oldTokens);
                                }
                                oldTokens[fila][columna].setPass(tokens[fila][columna].getPass());
                            }
                        }
                    }
                    tokens[select[1]][select[2]].moveToken(j, i);
                    if(tokens[select[1]][select[2]].getCorrectMove()){
                        int F = tokens[select[1]][select[2]].getRow();
                        int C = tokens[select[1]][select[2]].getColumn();
                        tokens[C][F] = tokens[select[1]][select[2]];
                        tokens[select[1]][select[2]] = null;
                        String[][] tablero = new String[n][n];
                        for(int column=0; column<n; column++){
                            for(int row=0; row<n; row++){
                                if (tokens[column][row]==null){
                                    tablero[(n-1)-row][column] = "";
                                }
                                else{
                                    if (tokens[column][row].getColor() == 0){
                                        tablero[(n-1)-row][column] = tokens[column][row].getType().toLowerCase();
                                    }
                                    else{
                                        tablero[(n-1)-row][column] = tokens[column][row].getType();
                                    }
                                }
                            }
                        }
                        if (!CheckJaque.checkJaquePropio(tokens[C][F].getColor(), tablero)){
                            if (select[3] == 0){
                                select[3] = 1;
                            }
                            else{
                                select[3] = 0;
                            }
                            try{
                                FileWriter fwriter = new FileWriter("logActual.txt", true);
                                fwriter.write((select[1]) + " " +  (7-(select[2])) + " " + (j) + " " +  (7-(i)) +"\n");
                                if (tokens[C][F].getType().equals("P") && (tokens[C][F].getRow() == 7 || tokens[C][F].getRow() == 0)){
                                    String pieza = "";
                                    while (pieza == null || pieza.length() == 0){
                                        Object[] possibilities = {"Reina", "Alfil", "Torre", "Caballo"};
                                        pieza = (String)JOptionPane.showInputDialog(null, "Elija la pieza a la cual promovera su peon:\n",
                                                            "Promocion", JOptionPane.PLAIN_MESSAGE, null, possibilities, "Reina");
                                        if ((pieza != null) && (pieza.length() > 0)) {
                                            if (pieza.equals("Reina")){
                                                tokens[C][F] = new Queen (C,F,tokens[C][F].getColor(),board,tokens);
                                            }
                                            else if (pieza.equals("Alfil")){
                                                tokens[C][F] = new Bishop (C,F,tokens[C][F].getColor(),board,tokens);
                                            }
                                            else if (pieza.equals("Torre")){
                                                tokens[C][F] = new Rook (C,F,tokens[C][F].getColor(),board,tokens);
                                            }
                                            else{
                                                tokens[C][F] = new Knight (C,F,tokens[C][F].getColor(),board,tokens);
                                            }
                                        }
                                    }
                                    fwriter.write(pieza +"\n");
                                }
                                fwriter.close();
                            }catch (Exception e){
                                System.err.println("Error: " + e.getMessage());
                            }
                        }
                        else{
                            for(int column=0; column<n; column++){
                                for(int row=0; row<n; row++){
                                    board[column][row].setIcon(null);
                                }
                            }
                            for(int column=0; column<n; column++){
                                for(int row=0; row<n; row++){
                                    tokens[column][row] = oldTokens[column][row];
                                    if (tokens[column][row]!=null){
                                        board[tokens[column][row].getColumn()][tokens[column][row].getRow()].setIcon(tokens[column][row].getIcon()[tokens[column][row].getColor()]);
                                    }
                                }
                            }
                        }
                    }
                }
                
            }
            select[0] = 0;
        }
        
        else if (select[0] == 0){
            if (tokens[j][i] != null){
                select[1] = j;
                select[2] = i;
                select[0] = 1;
            }
        } 
    }
}
